<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div>
    <div>原始文本：<input type="text" id="orgtxt" style="width: 400px;"></div>
    <div>加密文本：<input type="text" id="aestxt" style="width: 400px;"></div>
    <div><input type="button" value="加密" onclick="doAes()"> <input type="button" value="解密" onclick="doDes()"></div>
  </div>
  <script>
    function doAes() {
      var tmpTxt = document.getElementById("orgtxt").value;
      document.getElementById("aestxt").value = strToHexCharCode(tmpTxt);

    }
    function doDes() {
      var tmpTxt = document.getElementById("aestxt").value;
      document.getElementById("orgtxt").value = hexCharCodeToStr(tmpTxt);

    }
    function strToHexCharCode(str) {
      if (str === "")
        return "";
      var hexCharCode = [];
      hexCharCode.push("0x");
      for (var i = 0; i < str.length; i++) {
        hexCharCode.push((str.charCodeAt(i)).toString(16));
      }
      return hexCharCode.join("");
    }

    function hexCharCodeToStr(hexCharCodeStr) {
      var trimedStr = hexCharCodeStr.trim();
      var rawStr =
        trimedStr.substr(0, 2).toLowerCase() === "0x" ?
          trimedStr.substr(2) :
          trimedStr;
      var len = rawStr.length;
      if (len % 2 !== 0) {
        return "";
      }
      var curCharCode;
      var resultStr = [];
      for (var i = 0; i < len; i = i + 2) {
        curCharCode = parseInt(rawStr.substr(i, 2), 16);
        resultStr.push(String.fromCharCode(curCharCode));
      }
      return resultStr.join("");
    }
  </script>
</body>

</html>